#include<iostream>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>

int main(int argc,char* argv[])
{
    std::string serverip=argv[1];
    uint16_t serverport=std::stoi(argv[2]);


    int sockfd=socket(AF_INET,SOCK_STREAM,0);
    
    struct sockaddr_in server;
    memset(&server,0,sizeof(server));
    server.sin_family=AF_INET;
    server.sin_port=htonl(serverport);
    inet_pton(AF_INET,serverip.c_str(),&(server.sin_addr));

    //tcp客户端要不要bind，要不要显示的bind
    //客户端发起connect时候
    while(true)
    {
        int n=connect(sockfd,(struct sockaddr*)&server,sizeof(server));
        if(n<0)
        {
            std::cerr<<"connect error..."<<std::endl;
            return 1;
        }

        // while(true)
        // {
            std::string message;

            std::cout<<"please#";
            std::getline(std::cin,message);
            n=write(sockfd,message.c_str(),message.size());
            if(n<0)
            {
                std::cerr<<"write error..."<<std::endl;
            
            }
            char inbuffer[4096];
            n=read(sockfd,inbuffer,sizeof(inbuffer));
            if(n>0)
            {
                inbuffer[n]=0;
                std::cout<<inbuffer<<std::endl;
    
            }
            else{
                break;
            }
            
        // }
        close(sockfd);
        
    }
}